package com.itheima.controller.admin;

import com.itheima.common.constant.MessageConstant;
import com.itheima.common.entity.PageResult;
import com.itheima.common.entity.QueryPageBean;
import com.itheima.common.entity.Result;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.CheckItem;
import com.itheima.service.CheckgroupService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/checkgroup")
@Slf4j
public class CheckgroupController {

    @Autowired
    private CheckgroupService checkgroupService;

    /**
     *新增检查组
     * @return
     */
    @PostMapping("/add")
    public Result save(@RequestBody CheckGroup checkGroup, String checkitemIds){
        log.info("新增检查组：{}, 检查项IDS：{}", checkGroup.toString(),checkitemIds);
        checkgroupService.save(checkGroup, checkitemIds);
        return new Result(true, MessageConstant.ADD_CHECKGROUP_SUCCESS);
    }

    /**
     *检查组分页查询
     */
    @PostMapping("/findPage")
    public PageResult page(@RequestBody QueryPageBean queryPageBean){
        log.info("分页查询:{}", queryPageBean);
        return checkgroupService.pageQuery(queryPageBean);
    }

    /**
     * 删除检查组
     */
    @GetMapping("/deleteById")
    public Result deleteById(Integer id){
        log.info("删除检查组ID为：{}", id);
        checkgroupService.deleteById(id);
        return new Result(true, MessageConstant.DELETE_CHECKGROUP_SUCCESS);
    }

    /**
     * 查询检查组
     */
    @GetMapping("/findById")
    public Result findById(Integer id){
        log.info("查询检查组ID:{}", id);
        CheckGroup checkGroup = checkgroupService.findById(id);
        return new Result(true, MessageConstant.QUERY_CHECKGROUP_SUCCESS, checkGroup);
    }

    /**
     * 编辑检查组
     * @return
     */
    @PostMapping("/edit")
    public Result update(@RequestBody CheckGroup checkGroup, String checkitemIds){
        log.info("编辑检查组：{}, 检查项IDS：{}", checkGroup.toString(),checkitemIds);
        checkgroupService.update(checkGroup, checkitemIds);
        return new Result(true, MessageConstant.EDIT_CHECKGROUP_SUCCESS);
    }

    /**
     * 查询所有的检查组
     */
    @GetMapping("/findAll")
    public Result findAll(){
        List<CheckGroup> checkGroupList = checkgroupService.findAll();
        return new Result(true, MessageConstant.QUERY_CHECKGROUP_SUCCESS, checkGroupList);
    }

    /**
     * 查询套餐下的检查组
     */
    @GetMapping("/findCheckGroupIdsBySetmealId")
    public Result findCheckGroupIdsBySetmealId(Integer setmealId){
        List<Integer> ids = checkgroupService.findCheckGroupIdsBySetmealId(setmealId);
        return new Result(true, MessageConstant.QUERY_CHECKGROUP_SUCCESS, ids);
    }
}
